<?php
/*

Class Yahoo search
@author Rana

*/

class Yahoo {
	
	//All search request URLs start with the hostname and resemble the following sample:
	var $hostname;
	
	var $livesite; // = 'http://siteexplorer.search.yahoo.com/search?bwm=i&bwms=p&bwmf=u&fr=sfp&fr2=seo-rd-se&p=';
	
	//After the hostname are the service name and version number:
	var $service;
	
	//Next is the method followed by a question mark.
	var $method;
	var $baseurl;
	var $response;
	
	//appid  string (required)  The application ID. See Application IDs for more information.
	var $appid;
	
	//query  string (required)  The domain or path to get inlink data for. 
	var $query;
	
	//results  integer: default 50, max 100  The number of results to return.  
	var $results = 0;
	
	//start  integer: default 1  The starting result position to return (1-based). The finishing position (start + results - 1) cannot exceed 1000. 
	var $start = 1;
	
	//entire_site  no value (default), or 1  Specifies whether to provide results for the entire site, or just the page referenced by the query. If the query is not a domain URL (i.e. it contains path information, such as http://smallbusiness.yahoo.com/webhosting/), this parameter has no effect.  
	var $entire_site;
	
	//omit_inlinks  none (default), domain, or subdomain  If specified, inlinks will not be returned if they are from pages in the same domain/subdomain as the requested page. 
	var $omit_inlinks='none';
	
	//output  string: xml (default), json, php  The format for the output. If json is requested, the results will be returned in JSON format. If php is requested, the results will be returned in Serialized PHP format. 
	var $output = 'php';
	
	//callback  string  The name of the callback function to wrap around the JSON data. The following characters are allowed: A-Z a-z 0-9 . [] and _. If output=json has not been requested, this parameter has no effect. More information on the callback can be found in the Yahoo! Developer Network JSON Documentation.  
	var $callback;
	

	function Yahoo() {
		$this->hostname = $GLOBALS['yahoo_hostname'];
		$this->service = $GLOBALS['yahoo_service'];
		$this->method = $GLOBALS['yahoo_method'];
		$this->appid = $GLOBALS['yahoo_appid'];
		$this->livesite = $GLOBALS['yahoo_live'];

		//$this->baseurl = $this->hostname.$this->service.$this->method;
		$this->frame_link();
	}
	
	function frame_link() {

		$this->baseurl = $this->hostname.$this->service.$this->method;

		$this->baseurl .= 'appid='.$this->appid.'&';
		//$this->baseurl .= 'results='.$this->results.'&';
		//$this->baseurl .= 'start='.$this->start.'&';
		if (!empty($this->entire_site))
			$this->baseurl .= 'entire_site='.$this->entire_site.'&';
		if ($this->omit_inlinks != 'none')
			$this->baseurl .= 'omit_inlinks='.$this->omit_inlinks.'&';
		$this->baseurl .= 'output='.$this->output.'&';
		if (!empty($this->callback))
			$this->baseurl .= 'callback='.$this->callback.'&';
	}
	
	function get_response($url) {
		if (empty($url)) return false;
		
		//$this->livesite .= $url;
		//echo $this->baseurl.'query='.$url;
		$contents = file_get_contents($this->baseurl.'query='.$url);
		
		if ($contents !== false) {
			$object  = unserialize($contents);
			if ( $object !== false ) {
				//$this->response = $object;
				//echo "<pre>";print_r($object);
				return $object;
			}
		}
		
		return false;
	}
	
	function get_popularity($url) {
		$this->response = $this->get_response($url);
		
		if ( $this->response === false )
			return 0;
		else {
			return $this->response['ResultSet']['totalResultsAvailable'];
		}
	}

	//Yahoo Results
	function get_yahoo($url) {
		$url = $GLOBALS['yahoo_live'];
		//$url = urlencode($url);
		$contents = file_get_contents($url);
		$results = 0;
		
		if ($contents !== false) {
			//$regexp = "/Inlinks\s+([0-9]+)\s+\-\s+([0-9]+)\s*of\s+about\s+([0-9]+)/i";
			$regexp = $GLOBALS['yahoo_regexp'];
			
			//$contents = strip_tags($contents);
			
			if (preg_match($regexp, $contents, $matches) ) {
				//print_r($matches);
				if (isset($matches[1])) {
					$results = $matches[1];
				}
			} 
		
		}
		return $results;
	}

	//
	function get_url($value, $url) {
		$url = '<a href="'.$this->livesite.$url.'" title="Yahoo" class="link1" target="_blank">'.$value.'</a>';

		return $url;
	}

	//
	function get_links() {

		$results = array();
		$i = 0;
		//echo "<pre>"; print_r($this->response);

		foreach ($this->response['ResultSet']['Result'] as $link) {

				$results[$i]['link'] = $link['Url'];
				$results[$i]['atext'] = $link['Title'];
				list($www,$wwwN) = get_domain($results[$i]['link']);
				$results[$i]['ip'] = gethostbyname($wwwN);

				$i++;
		}
		return $results;
	}
}

?>